﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Sw.Hospital.HealthExaminationSystem.AI
{
    public class AiPost
    {
        public static string AiPostData(string message)
        {
            string strURL = "http://open.bigmodel.cn/api/paas/v4/chat/completions";
            //创建一个HTTP请求  
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            //Post请求方式  
            request.Method = "POST";
            //内容类型
            request.ContentType = "application/json";
            request.Headers.Add("Authorization", "Bearer 22e901581ad8fd3a75894f86e66bb7a8.XAGHX2MBp5nmJG2j");
            //设置参数，并进行URL编码 

            string paraUrlCoded = "{\"model\": \"glm-4\", \"messages\": [ {\"role\": \"user\",\"content\": \"" + message + "\"  }   ] }";//System.Web.HttpUtility.UrlEncode(jsonParas);   

            byte[] payload;
            //将Json字符串转化为字节  
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            //设置请求的ContentLength   
            request.ContentLength = payload.Length;
            //发送请求，获得请求流 

            Stream writer;
            try
            {
                writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
            }
            catch (Exception)
            {
                writer = null;
                Console.Write("连接服务器失败!");
            }
            //将请求参数写入流
            writer.Write(payload, 0, payload.Length);
            writer.Close();//关闭请求流
                           // String strValue = "";//strValue为http响应所返回的字符流
            HttpWebResponse response;
            try
            {
                //获得响应流
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
                return "";
            }
            Stream s = response.GetResponseStream();
            //  Stream postData = Request.InputStream;
            StreamReader sRead = new StreamReader(s);
            string postContent = sRead.ReadToEnd();

            sRead.Close();

            var aiMsg = JsonConvert.DeserializeObject<AiMessageModel>(postContent);
            //返回建议
            return aiMsg.choices[0].message.content;

            //return postContent;//返回Json数据
        }
        public static string AiPostJsonData(string json)
        {
            string strURL = "http://open.bigmodel.cn/api/paas/v4/chat/completions";
            //创建一个HTTP请求  
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            //Post请求方式  
            request.Method = "POST";
            //内容类型
            request.ContentType = "application/json";
            request.Headers.Add("Authorization", "Bearer 22e901581ad8fd3a75894f86e66bb7a8.XAGHX2MBp5nmJG2j");
            //设置参数，并进行URL编码 

            string paraUrlCoded = json;//System.Web.HttpUtility.UrlEncode(jsonParas);   

            byte[] payload;
            //将Json字符串转化为字节  
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            //设置请求的ContentLength   
            request.ContentLength = payload.Length;
            //发送请求，获得请求流 

            Stream writer;
            try
            {
                writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
            }
            catch (Exception)
            {
                writer = null;
                Console.Write("连接服务器失败!");
            }
            //将请求参数写入流
            writer.Write(payload, 0, payload.Length);
            writer.Close();//关闭请求流
                           // String strValue = "";//strValue为http响应所返回的字符流
            HttpWebResponse response;
            try
            {
                //获得响应流
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
                return "";
            }
            Stream s = response.GetResponseStream();
            //  Stream postData = Request.InputStream;
            StreamReader sRead = new StreamReader(s);
            string postContent = sRead.ReadToEnd();

            sRead.Close();

            var aiMsg = JsonConvert.DeserializeObject<AiMessageModel>(postContent);
            //返回建议
            return aiMsg.choices[0].message.content;

            //return postContent;//返回Json数据
        }
    }
    /// <summary>
    /// 发送信息列表
    /// </summary>
    public class SendAiMessageListModel
    {
        /// <summary>
        /// 
        /// </summary>
        public string model { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<Message> messages { get; set; }
    }

    public class AiMessageModel
    {
        /// <summary>
        /// 
        /// </summary>
        public List<ChoicesItem> choices { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int created { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string id { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string model { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string request_id { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Usage usage { get; set; }
    }

    public class Usage
    {
        /// <summary>
        /// 
        /// </summary>
        public int completion_tokens { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int prompt_tokens { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int total_tokens { get; set; }
    }
    public class Message
    {
        /// <summary>
        /// 建议内容
        /// </summary>
        public string content { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string role { get; set; }
    }

    public class ChoicesItem
    {
        /// <summary>
        /// 
        /// </summary>
        public string finish_reason { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int index { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Message message { get; set; }
    }
}
